package Reflection.Field;

import java.lang.reflect.Field;

/**
 * @author zxc
 * @date 2023/04/07 23:42
 **/
public class SetAndGetDemo {
    public static void main(String[] args) throws ClassNotFoundException,
            NoSuchFieldException, IllegalAccessException, InstantiationException {
//        Object get(Object obj) 返回由该 Field表示的字段在指定对象上的值。
        method1();
        method2();
    }

    private static void method2() throws ClassNotFoundException,
            NoSuchFieldException, InstantiationException, IllegalAccessException {
        //1.获取class对象
        Class clazz = Class.forName("Reflection.Field.Student");
        //2.获取成员变量Field的对象
        Field field = clazz.getDeclaredField("money");
        //3.取消访问检查
        field.setAccessible(true);
        //4.调用get方法来获取值
        //4.1创建一个对象
        Student student = (Student) clazz.newInstance();
        //4.2获取指定对象的money的值
        Object o = field.get(student);
        //5.打印一下
        System.out.println(o);
    }
    // 给所获取到的成员变量，进行赋值操作;
    // 若是所获取的成员变量是被private访问修饰符进行修饰，则调用setAccessible方法，取消该成员变量的访问检查;
    private static void method1() throws ClassNotFoundException,
            NoSuchFieldException, InstantiationException, IllegalAccessException {
        // void set(Object obj, Object value)：给obj对象的成员变量赋值为value
        //1.获取class对象
        Class clazz = Class.forName("Reflection.Field.Student");

        //2.获取name这个Field对象
        Field fieldname = clazz.getField("name");
        Field fieldage = clazz.getField("age");

        //3.利用set方法进行赋值.
        //3.1先创建一个Student对象
        Student student = (Student) clazz.newInstance();
        //3.2有了对象才可以给指定对象进行赋值
        fieldname.set(student,"杨天真");
        fieldage.set(student,12);

        System.out.println(student);
    }
}
